package com.chii.colorization.ui.fragment.MainActivity.ai;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.chii.colorization.R;
import com.chii.colorization.ui.activity.ColorizationActivity;
import com.chii.colorization.ui.activity.MainActivity;
import com.chii.colorization.utils.SPUtil;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class AiFragment extends Fragment {

    @BindView(R.id.text_ai)
    TextView textView;
    @BindView(R.id.image_colorization)
    CardView imageColorization;
    @BindView(R.id.image_colorization_imageView)
    ImageView imageColorizationImageView;
    @BindView(R.id.image_colorization_title)
    TextView imageColorizationTitle;
    private Handler mHandler;
    private static JSONObject config;
    private static List<ImageView> Image_VIEW_LIST;
    private static List<String> Image_TITLE_LIST;
    //设置值
    LocalBroadcastManager broadcastManager;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_ai, container, false);
        ButterKnife.bind(this, root);

        initHandler();
        //通过网络获取初始化设置
        initConfig();
        return root;
    }



    private void initConfig() {
        broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter filter = new IntentFilter();
        filter.addAction("chii.init.config");
        broadcastManager.registerReceiver(receiver, filter);
        if (config != null) {
            initData(getContext());
            initUI(getContext());
        }
    }

    // broadcast receiver
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("chii.init.config")) {
                Message message = new Message();
                message.what = 1;
                message.obj = context;
                mHandler.sendMessage(message);
            }
        }
    };

    private void initData(Context context) {
        String CONFIG = (String) SPUtil.get(context, "config", "");

        config = JSON.parseObject(CONFIG);
        Image_VIEW_LIST = Arrays.asList(
                imageColorizationImageView
        );
        Image_TITLE_LIST = Arrays.asList(
                "colorization"
        );
    }

    private void initUI(Context context) {
        for (int i = 0; i < Image_TITLE_LIST.size(); i++) {
            String url = config.getJSONObject(Image_TITLE_LIST.get(i)).getString("image");
            Glide.with(context)
                    .load(url)
                    .dontAnimate()
                    .placeholder(R.drawable.ic_autorenew_black)
                    .into(Image_VIEW_LIST.get(i));
        }
    }

    private void initHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        initData((Context) msg.obj);
                        initUI((Context) msg.obj);
                        break;
                    default:
                }
                return false;
            }
        });
    }

    @OnClick({R.id.text_ai, R.id.image_colorization})
    public void onViewClicked(View view) {
        if (config == null) {
            Toast.makeText(getContext(), "无法打开，请连接服务器！！", Toast.LENGTH_LONG).show();
            return;
        }
        String token = (String) SPUtil.get(getContext(), "token", "");
        if ("".equals(token)) {
            Toast.makeText(getContext(), "无法打开，请先登录！！", Toast.LENGTH_LONG).show();
            return;
        }
        switch (view.getId()) {
            case R.id.text_ai:
                break;
            case R.id.image_colorization:
                Toast.makeText(getActivity(), "打开黑白上色", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(getActivity(), ColorizationActivity.class);
                startActivity(intent);
                break;
            default:
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}